package com.yzl.meeting.business.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yzl.meeting.business.rule.DataResult;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.UUID;

public class CommonsUtils {

    public static String getUUID(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
    public static String getUserInf(HttpServletRequest req,String attribute) throws Exception {
        Cookie[] cookies = req.getCookies();
        String result = null;
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("userInfo")) {
                    String value = cookie.getValue();
                    System.out.println(value);
                    String decode = URLDecoder.decode(value, "UTF-8");
                    System.out.println(decode);
                    ObjectMapper objectMapper = new ObjectMapper();
                    DataResult dr = (DataResult) objectMapper.readValue(decode, DataResult.class);
                    Object t = dr.getT();
                    LinkedHashMap t1 = (LinkedHashMap) t;
                    result = (String) t1.get(attribute);
                }
            }
        }
        return result;
    }
}
